package com.ruoyi.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.domain.TbShopDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.TbShopMapper;
import com.ruoyi.shop.domain.TbShop;
import com.ruoyi.shop.service.ITbShopService;

/**
 * 商铺管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class TbShopServiceImpl implements ITbShopService
{
    @Autowired
    private TbShopMapper tbShopMapper;

    /**
     * 查询商铺管理
     * 
     * @param id 商铺管理主键
     * @return 商铺管理
     */
    @Override
    public TbShop selectTbShopById(String id)
    {
        return tbShopMapper.selectTbShopById(id);
    }

    /**
     * 查询商铺管理列表
     * 
     * @param tbShop 商铺管理
     * @return 商铺管理
     */
    @Override
    public List<TbShopDto> selectTbShopList(TbShop tbShop)
    {
      List<TbShopDto> newlist = new ArrayList<>();
      List<TbShop> list = tbShopMapper.selectTbShopList(tbShop);
      for(TbShop shopOne:list){
          TbShopDto shopDto = new TbShopDto();
          String type_id = shopOne.getTypeId();
          String typeName = tbShopMapper.selectTbShopTypeById(type_id);
          shopDto.setTypeName(typeName);
          shopDto.setId(shopOne.getId());
          shopDto.setName(shopOne.getName());
          shopDto.setTypeId(shopOne.getTypeId());
          shopDto.setImages(shopOne.getImages());
          shopDto.setArea(shopOne.getArea());
          shopDto.setAddress(shopOne.getAddress());
          shopDto.setAvgPrice(shopOne.getAvgPrice());
          shopDto.setSold(shopOne.getSold());
          shopDto.setComments(shopOne.getComments());
          shopDto.setScore(shopOne.getScore());
          shopDto.setOpenHours(shopOne.getOpenHours());
          shopDto.setStatus(shopOne.getStatus());
          shopDto.setIsDeleted(shopOne.getIsDeleted());
          newlist.add(shopDto);
      }
      return newlist;
    }

    /**
     * 新增商铺管理
     * 
     * @param tbShop 商铺管理
     * @return 结果
     */
    @Override
    public int insertTbShop(TbShop tbShop)
    {
        tbShop.setCreateTime(DateUtils.getNowDate());
        return tbShopMapper.insertTbShop(tbShop);
    }

    /**
     * 修改商铺管理
     * 
     * @param tbShop 商铺管理
     * @return 结果
     */
    @Override
    public int updateTbShop(TbShop tbShop)
    {
        tbShop.setUpdateTime(DateUtils.getNowDate());
        return tbShopMapper.updateTbShop(tbShop);
    }

    /**
     * 批量删除商铺管理
     * 
     * @param ids 需要删除的商铺管理主键
     * @return 结果
     */
    @Override
    public int deleteTbShopByIds(String[] ids)
    {
        return tbShopMapper.deleteTbShopByIds(ids);
    }

    /**
     * 删除商铺管理信息
     * 
     * @param id 商铺管理主键
     * @return 结果
     */
    @Override
    public int deleteTbShopById(String id)
    {
        return tbShopMapper.deleteTbShopById(id);
    }

    @Override
    public boolean updateShopStatus(Long id, Integer status) {
        // 验证状态值是否合法
        if (status != 0 && status != 1) {
            throw new IllegalArgumentException("状态值不合法");
        }
        return tbShopMapper.updateShopStatus(id, status) > 0;
    }
}
